package run.siyuan.manag.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;

/**
 * @program: sy-auth-manag
 * @description:
 * @author: SY
 * @create: 2021-01-14 15:20
 **/
@Configuration
@EnableWebSecurity // 开启 spring security
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启权限注解，@PreAuthorize
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 指定身份验证，也可以使用直接设置帐号
     * eg:        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN","USER")
     *                                   .and().withUser("user").password("user").roles("USER");
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 因为 自定义了身份验证方式，所以我们使用自定义身份验证，否则使用一统默认的 auth.userDetailsService(userDetailsService);
        auth.authenticationProvider(new JwtAuthenticationProvider(userDetailsService));
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 禁用 csrf 由于使用的是JWT,这里不需要csrf
        http.cors().and().csrf().disable().authorizeRequests()
                // 跨域请求
                .antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
                // web jars
                .antMatchers("/webjars/**").permitAll()
                // sql 监控
                .antMatchers("/druid/**").permitAll()
                // 首页和登入页面
                .antMatchers("/").permitAll()
                .antMatchers("/login").permitAll()
                // knife4j
                .antMatchers("/doc.html").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/v2/api-docs").permitAll()
                .antMatchers("/webjars/springfox-swagger0ui/**").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                // 验证码
                .antMatchers("/render").permitAll()
                // 监控
                .antMatchers("/actuator/**").permitAll()
                //
                .anyRequest().authenticated();
        // 退出登入
        http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
        http.addFilterBefore(new JwtAuthenticationFilter(authenticationManager()), UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
        protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
